//
//  VMCBasicViewController.m
//  vmcshop
//
//  Created by toggle on 15/6/4.
//  Copyright (c) 2015年 idongler. All rights reserved.
//

#import "VMCBasicViewController.h"

@interface VMCBasicViewController () <UIGestureRecognizerDelegate>


@property (assign, nonatomic) CGFloat navHeight;

@property (strong, nonatomic) UITapGestureRecognizer *tapGesture;

@end

@implementation VMCBasicViewController

- (id)init
{
	self = [super init];
	if (self) {
		_isShowBackItem = NO;
		_isShowRightItem = NO;
		_navBarHide = NO;
		_backButton = nil;
		_rightButton = nil;
		_navBar = nil;
		_navHeight = 64.0f;
		_tapGesture = nil;
	}
	return self;
}

- (void)viewDidLoad
{
	[super viewDidLoad];
	self.view.backgroundColor = RGB(236, 236, 236);
	[self setUpNavBar];
	[self setNavBarHide:_navBarHide animated:NO];
	
	if(self.navigationController.viewControllers.count > 1)
	{
		[self addBackItem];
	}
	[self addRightItem];
}

- (void)addRightItem
{
	_rightButton = [[UIButton alloc] init];
	[_rightButton addTarget:self action:@selector(rightItemTouched:) forControlEvents:UIControlEventTouchUpInside];
	_rightButton.frame = CGRectMake(CGRectGetWidth(self.view.frame)-40-15, 20+44/2-30/2, 40, 30);

	//[_rightButton setImage:[UIImage imageNamed:@"goodsAddBtn"] forState:UIControlStateNormal];
	[_navBar addSubview:_rightButton];

}

- (void)setTitle:(NSString *)title
{
	[super setTitle:title];
	_navBar.titleLabel.text = title;
}

- (void)addBackItem
{
	_backButton = [[UIButton alloc] init];
	[_backButton addTarget:self action:@selector(backItemTouched:) forControlEvents:UIControlEventTouchUpInside];
	_backButton.frame = CGRectMake(0, 20+44/2-40/2, 40, 40);
	
	[_backButton setImage:[UIImage imageNamed:@"icon_arrow_left_40"] forState:UIControlStateNormal];
	[_navBar addSubview:_backButton];
	
	UITapGestureRecognizer *navTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(navTapGestureTaped:)];
	[_navBar addGestureRecognizer:navTapGesture];
}

- (void)navTapGestureTaped:(UITapGestureRecognizer *)gesture
{
	CGPoint point = [gesture locationInView:_navBar];
	if(point.x < CGRectGetWidth(_navBar.frame) / 3.0f)
	{
		[self backItemTouched:nil];
	}
}

- (void)backItemTouched:(id)sender
{
	[self.navigationController popViewControllerAnimated:YES];
}

- (void)rightItemTouched:(id)sender
{
	
}

- (void)setUpNavBar
{
	CGSize bdSize = [UIScreen mainScreen].applicationFrame.size;
	_navBar = [[VMCNavigationBar alloc] initWithFrame:CGRectMake(0, 0, bdSize.width, _navHeight)];
	_navBar.titleLabel.text = self.title;
	[self.view addSubview:_navBar];
}

- (void)setNavBarHide:(BOOL)navBarHide animated:(BOOL)animated
{
	_navBarHide = navBarHide;
	if(_navBarHide)
	{
		[self hideNavBarAnimated:animated];
	}
	else
	{
		[self showNavBarAnimated:animated];
	}
}

- (void)showNavBarAnimated:(BOOL)animated
{
	CGSize bdSize = [UIScreen mainScreen].applicationFrame.size;
	_navBar.hidden = NO;
	[UIView animateWithDuration:0.2f * !animated animations:^(void){
		_navBar.center = CGPointMake(bdSize.width / 2.0f, _navHeight / 2.0f);
	}];
}

- (void)hideNavBarAnimated:(BOOL)animated
{
	CGSize bdSize = [UIScreen mainScreen].applicationFrame.size;
	[UIView animateWithDuration:0.2f * !animated animations:^(void){
		_navBar.center = CGPointMake(bdSize.width / 2.0f, -_navHeight / 2.0f);
	} completion:^(BOOL finished){
		_navBar.hidden = YES;
	}];
}

#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
	if([touch.view isKindOfClass:[UIControl class]])
	{
		return (NO);
	}
	return (YES);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
